#include "sysdefs.h"

int main() {

	int idFilaEnvio;
	int idFilaRecebimento;
	intMsg dummy;
	int i;
	int j;

	procTable receivedTable;

	dummy.dummyLong = getpid();
	dummy.intMsg = 0;

	/*Obtem a fila para o schedule*/
	if ((idFilaEnvio = msgget(CHAVE_FILA_LIST_SCHEDULE, 0x1ff)) < 0) {
		printf("SO List: Erro ao tentar obter fila de mensagens!\n");
		exit(1);
	}

	if ((idFilaRecebimento = msgget(CHAVE_FILA_SCHEDULE_LIST, 0x1ff)) < 0) {
		printf("SO List: Erro ao tentar obter fila de mensagens!\n");
		exit(1);
	}

	if (msgsnd(idFilaEnvio, &dummy, sizeof(intMsg)-sizeof(long), 0) < 0) {
		printf("SO List: Erro ao enviar requisicao de informacoes de processos!\n");
		exit(1);
	}

	if (msgrcv(idFilaRecebimento, &receivedTable, sizeof(procTable)-sizeof(long), 0, 0) < 0) {
		printf("SO List: Erro ao receber mensagem do schedule com as informacoes dos processos!\n");
		exit(1);
	}

	printf("Nreq\tMax_Time\tN_proc\tStart_Time\tStatus\tProgram\n");

	for (i = 0; i < receivedTable.numProcesses; i++) {
		printf("%d\t%d:%d:%d\t\t%d\t%d:%d:%d\t\t%s\t%s ", receivedTable.procInfoArray[i].nReq, receivedTable.procInfoArray[i].execSeconds / 3600, (receivedTable.procInfoArray[i].execSeconds / 60) % 60, receivedTable.procInfoArray[i].execSeconds % 60, receivedTable.procInfoArray[i].numProc,
				receivedTable.procInfoArray[i].startTime / 3600, (receivedTable.procInfoArray[i].startTime / 60) % 60, receivedTable.procInfoArray[i].startTime % 60, receivedTable.procInfoArray[i].state == RUNNING ? "RUNNING" : "PENDING", receivedTable.procInfoArray[i].programName);
		
		for (j = 1; j < receivedTable.procInfoArray[i].argCount; j++) {
			printf("%s ", receivedTable.procInfoArray[i].programArgs[j]);
		}
		printf("\n");
	}

	return 0;
}

